import derelict.sdl.sdl;
import std.math;
import Pixels;

void circlePoints(SDL_Surface *surface,int x0, int y0, int x, int y, uint value){
    putPixel(surface, x0 + x, y0 + y, value);
    putPixel(surface, x0 + y, y0 + x, value);
    putPixel(surface, x0 + y, y0 - x, value);
    putPixel(surface, x0 + x, y0 - y, value);
    putPixel(surface, x0 - x, y0 - y, value);
    putPixel(surface, x0 - y, y0 - x, value);
    putPixel(surface, x0 - y, y0 + x, value);
    putPixel(surface, x0 - x, y0 + y, value);
}

void midPointFloatCircle(SDL_Surface *surface, int x0, int y0, int radius, uint value){
	int x,y;
	float d;

	x = 0;
	y = radius;
	d = 5.f / 4 - radius;
	circlePoints(surface, x0, y0, x, y, value);
	while(y > x){
		if(d < 0){
			d += x*2.f + 3;
			x++;
		}
		else{
			d += (x-y) * 2.f + 5;
			x++;
			y--;
		}
		circlePoints(surface, x0, y0, x, y, value);
	}
}

void midPointCircle(SDL_Surface *surface, int x0, int y0, int radius, uint value){
	int x, y, d;

	x = 0;
	y = radius;
	d = 1 - radius;
	circlePoints(surface, x0, y0, x, y, value);
	while(y > x){
		if(d < 0){
			d += x*2 + 3;
			x++;
		}
		else{
			d += (x-y)*2 + 5;
			x++;
			y--;
		}
		circlePoints(surface, x0, y0, x, y, value);
	}
}

void midPointCircle2(SDL_Surface *surface, int x0, int y0, int radius, uint value){
	int x, y, d;
	int deltaE, deltaSE;

	x = 0;
	y = radius;
	d = 1 - radius;
	deltaE = 3;
	deltaSE = 5 - radius * 2;
	circlePoints(surface, x0, y0, x, y, value);
	while(y > x){
		if(d < 0){
			d += deltaE;
			deltaE += 2;
			deltaSE += 2;
			x++;
		}
		else{
			d += deltaSE;
			deltaE += 2;
			deltaSE += 4;
			x++;
			y--;
		}
		circlePoints(surface, x0, y0, x, y, value);
	}
}

void ellipse(SDL_Surface *surface, int x1, int y1, int x2, int y2, uint color)
{
   float t, a, b, tinc, centx, centy;
   a = abs(.5 * cast(float)(x2 - x1));
   b = abs(.5 * cast(float)(y2 - y1));
   tinc = PI * 2 / (a + b);
   centx = cast(float)((x1 + x2) + .5) * .5;
   centy = cast(float)((y1 + y2) + .5) * .5;

//   _moveto(centx + a, centy);
   for(t = 0; t < PI * 2; t += tinc)
      putPixel(surface, cast(int)(centx + a * cos(t)), cast(int)(centy - b * sin(t)), color);
}

alias midPointCircle2 circle;
